package com.yunshipei.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.yunshipei.EnterApplication;
import com.yunshipei.common.Globals;
import com.yunshipei.core.utils.CommonUtils;
import com.yunshipei.enterplorer.R;
import com.yunshipei.inter.ModifyPasCallBack;
import com.yunshipei.manager.YspPreferences;
import com.yunshipei.model.NotifyInfo;
import com.yunshipei.model.ShareInfoModel;
import com.yunshipei.ui.fragment.BrowserFragment;
import com.yunshipei.ui.fragment.CollectionFragment;
import com.yunshipei.ui.view.CollectionLongPressDialog;
import com.yunshipei.ui.view.File2HtmlChangeDialog;
import com.yunshipei.utils.BaseUtil;
import com.yunshipei.utils.EncUtils;
import com.yunshipei.utils.ToastUtils;

import io.rong.imlib.model.Conversation;

public class DialogHelper {

    public static CommonDialog getCancelableCommonDialog(Context context) {
        CommonDialog dialog = new CommonDialog(context, R.style.dialog_common);
        dialog.setCanceledOnTouchOutside(true);
        return dialog;
    }

    public static WaitDialog getWaitDialog(Context context, String message) {
        WaitDialog dialog = null;
        try {
            dialog = new WaitDialog(context, R.style.dialog_waiting);
            dialog.setMessage(message);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return dialog;
    }

    public static WaitDialog getNoCancelableWaitDialog(Context context, String message) {
        WaitDialog dialog = getWaitDialog(context, message);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                return keyCode == KeyEvent.KEYCODE_BACK;
            }
        });
        return dialog;
    }

    public static File2HtmlChangeDialog getFileConverterDialog(Context context) {
        File2HtmlChangeDialog mLoading = new File2HtmlChangeDialog(context, com.yunshipei.enterplorer.R.style.MyDialogStyle);
        mLoading.setCanceledOnTouchOutside(false);
        mLoading.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                return keyCode == KeyEvent.KEYCODE_BACK;
            }
        });
        return mLoading;
    }

    public static SetupPhotoDialog getSetupPhotoDialog(Context context) {
        return new SetupPhotoDialog(context);
    }

    public static NotifiDestoryDeleteDialog getNotifiDestoryDeleteDialog(Context context, NotifyInfo notifiInfo) {
        return new NotifiDestoryDeleteDialog(context, R.style.cike_alert_dialog, notifiInfo);
    }

    public static void showQuitGroup(Context context, DialogInterface.OnClickListener listener) {
        CommonDialog commonDialog = new CommonDialog(context);
        commonDialog.setTitle("提示");
        commonDialog.setMessage("确定退出该群组?");
        commonDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        commonDialog.setPositiveButton("确定", listener);
        commonDialog.show();
    }

    public static void showCheckMassTextDialog(Context context) {
        final CommonDialog commonDialog = new CommonDialog(context);
        commonDialog.setMessage("最多选择10个联系人");
        commonDialog.setPositiveButton("我知道了", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                commonDialog.dismiss();
            }
        });
        commonDialog.show();
    }

    public static void showMultiDevicesError(final Context context) {
        CommonDialog commonDialog = getAlertDialog(context, "警告", "该账号已在其他设备登录", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                EnterApplication.getApp().onTerminate();
            }
        });
        commonDialog.setCancelable(false);
        commonDialog.setCanceledOnTouchOutside(false);
        commonDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                return keyCode == KeyEvent.KEYCODE_BACK;
            }
        });
        commonDialog.show();
    }

    public static void showError(Context context, String message) {
        showAlert(context, "提示", message, null);
    }

    public static void showAlert(Context context, String title, String message, DialogInterface.OnClickListener clickListener) {
        getAlertDialog(context, title, message, clickListener).show();
    }

    public static CommonDialog getAlertDialog(Context context, String title, String message, DialogInterface.OnClickListener clickListener) {
        final CommonDialog commonDialog = new CommonDialog(context);
        commonDialog.setTitle(title);
        commonDialog.setMessage(message);
        commonDialog.setNegativeButton("", null);
        commonDialog.setPositiveButton("确认", clickListener == null ? new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        } : clickListener);
        return commonDialog;
    }

    public static CommonDialog getAlertDialogCancelable(Context context, String message, DialogInterface.OnClickListener clickListener) {
        final CommonDialog commonDialog = new CommonDialog(context);
        commonDialog.setTitle("提示");
        commonDialog.setMessage(message);
        commonDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        commonDialog.setPositiveButton("确认", clickListener == null ? new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        } : clickListener);
        return commonDialog;
    }

    public static void checkPasswordDialog(final Context context, final ModifyPasCallBack callBack) {
        CommonDialog commonDialog = new CommonDialog(context);
        commonDialog.setTitle("");
        View view = View.inflate(context, R.layout.dialog_check_password, null);
        final EditText etModifyPassword = (EditText) view.findViewById(R.id.et_old_password);
        commonDialog.setContent(view);
        commonDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        commonDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String pas = BaseUtil.replaceBlank(etModifyPassword.getText().toString());
                if (TextUtils.isEmpty(pas)) {
                    ToastUtils.showToast("请输入原密码");
                    return;
                }
                SharedPreferences preferences = YspPreferences.getInstance().getSharedPreferences();
                String oldPassword = EncUtils.decrypt(preferences.getString(Globals.UUID, ""), preferences.getString(Globals.PASSWORD, ""));
                dialog.dismiss();
                if (pas.equals(oldPassword)) {
                    dialog.dismiss();
                    callBack.oldPasCheckFinished();
                    return;
                }
                //不相等
                ToastUtils.showToast("原密码错误");
            }
        });
        commonDialog.show();
    }

    public static void clearAllTabDialog(Context context, DialogInterface.OnClickListener listener) {
        CommonDialog commonDialog = new CommonDialog(context);
        commonDialog.setTitle("");
        commonDialog.setMessage("<html>确定关闭所有页面<br/></html>");
        commonDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        commonDialog.setPositiveButton("确定", listener);
        commonDialog.show();
    }

    public static void deleteGroupMemberDialog(Context context, String content, DialogInterface.OnClickListener listener) {
        final CommonDialog commonDialog = new CommonDialog(context);
        commonDialog.setTitle("");
        commonDialog.setMessage(content);
        commonDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                commonDialog.dismiss();
            }
        });
        commonDialog.setPositiveButton("确定", listener);
        commonDialog.show();
    }

    public static class ShareDialog {
        Context mContext;
        View view = null;
        ImageView avator;
        TextView title, tvUrl, htmlTitle;
        ShareInfoModel shareInfoModel = null;
        EditText sendEdit;
        CommonDialog commonDialog = null;

        public ShareDialog(Context context) {
            mContext = context;
            view = LayoutInflater.from(context).inflate(R.layout.share_dialog_view, null);
            avator = (ImageView) view.findViewById(R.id.avator);
            title = (TextView) view.findViewById(R.id.share_title);
            tvUrl = (TextView) view.findViewById(R.id.share_url);
            htmlTitle = (TextView) view.findViewById(R.id.html_title);
            sendEdit = (EditText) view.findViewById(R.id.send_content);
            sendEdit.setFilters(new InputFilter[]{new InputFilter.LengthFilter(256)});
            sendEdit.clearFocus();
        }

        public String getEditValue() {
            return sendEdit.getText().toString();
        }

        public EditText getSendEdit() {
            return sendEdit;
        }

        public ShareInfoModel getSendEidtValue() {
            return this.shareInfoModel;
        }

        public void setData(ShareInfoModel data) {
            if (data == null)
                return;
            this.shareInfoModel = data;
            if (data.getType() == Conversation.ConversationType.GROUP) {
                Glide.with((Activity) mContext).load(data.getShareModel().getAvator()).placeholder(R.drawable.group).into(avator);
                title.setText(data.getShareModel().getName() + " (" + data.getShareModel().getNember() + "人)");
            } else if (data.getType() == Conversation.ConversationType.PRIVATE) {
                Glide.with((Activity) mContext).load(data.getShareModel().getAvator()).placeholder(R.drawable.group).into(avator);
                title.setText(data.getShareModel().getName());
            }
            htmlTitle.setText(data.getHtmlTitle());
            tvUrl.setText(data.getUrl());
        }


        public void showDialog(DialogInterface.OnClickListener onClickListener) {
            commonDialog = new CommonDialog(mContext);
            commonDialog.setContent(view);
            commonDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(sendEdit.getWindowToken(), 0);
                    dialog.dismiss();
                }
            });
            commonDialog.setPositiveButton("确定", onClickListener);
            commonDialog.show();
        }

        public void cancel() {
            commonDialog.cancel();
        }
    }

    public static void showHomeIntroduceDialog(Context context) {
        new HomeIntroduceDialog(context).show();
    }

    public static XCloudLoading getXCloudLoading(Context context) {
        XCloudLoading xCloudLoading = new XCloudLoading(context, R.style.xcloud_loading_dialog);
        xCloudLoading.setCancelable(false);
        xCloudLoading.setCanceledOnTouchOutside(false);
        xCloudLoading.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                return keyCode == KeyEvent.KEYCODE_BACK;
            }
        });
        return xCloudLoading;
    }

    public static void showDeleteCollectionDialog(Context context, String title, final CollectionFragment.CancelCollectionCallBack callBack) {
        final CollectionLongPressDialog dialog = new CollectionLongPressDialog(context, R.style.MyDialogStyle);
        dialog.setCanceledOnTouchOutside(true);
        View customView = dialog.getCustomView();
        TextView tvTitle = (TextView) customView.findViewById(R.id.collection_title);
        TextView del = (TextView) customView.findViewById(R.id.del_collection);
        tvTitle.setText(title);
        del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.removeCollection();
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    public static void showLicenseChangeAlert(Context context, String message, DialogInterface.OnClickListener listener) {
        CommonDialog commonDialog = new CommonDialog(context);
        commonDialog.setTitle("");
        commonDialog.setMessage(message);
        commonDialog.setNegativeButton("", null);
        commonDialog.setPositiveButton("确定", listener);
        commonDialog.setCancelable(false);
        commonDialog.setCanceledOnTouchOutside(false);
        commonDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                return keyCode == KeyEvent.KEYCODE_BACK;
            }
        });
        commonDialog.show();
    }


    public static void showHttpAuthRequest(final Context context, String vpnName, final BrowserFragment.VPNLoginRequest request) {
        CommonDialog commonDialog = new CommonDialog(context);
        commonDialog.setTitle(vpnName);
        View contentView = View.inflate(context, R.layout.dialog_vpn_login, null);
        commonDialog.setContent(contentView);
        final EditText etUserName = (EditText) contentView.findViewById(R.id.vpn_user_name);
        final EditText etPassword = (EditText) contentView.findViewById(R.id.vpn_password);
        commonDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                request.errorData();
            }
        });
        commonDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String userName = CommonUtils.replaceBlank(etUserName.getText().toString());
                String password = CommonUtils.replaceBlank(etPassword.getText().toString());
                if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(password)) {
                    Toast.makeText(context, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                dialog.dismiss();
                request.loginRequest(userName, password);
            }
        });
        commonDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                CommonUtils.hideKeyBoard(context);
            }
        });
        commonDialog.show();
    }
}
